
const config = require('../../../config/api.js');
Page({

  data: {
    currentTab: 0,
    waitReceive: [],
    waitPay: [],
    ing: [],
    end: [],
    currentPay: null,
    currentIng: null
  },

 
  onLoad: function(options) {
    this.getMyTask();
    this.getIntegral();
  },

  getIntegral: function () {
    wx.request({
      url: config.integralUrl,
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        this.setData({
          completePoint: res.data.complete_point
        })
      }
    })
  },
  getMyTask: function() {
    wx.request({
      url: config.getMyTask,
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res);
        this.setData({
          waitReceive: res.data.wait_receive,
          waitPay: res.data.wait_pay,
          ing: res.data.ing,
          end: res.data.end
        })
      }
    })
  },

  // 取消任务申请退款
  cancelTask: function(e) {
    let that = this;
    let taskid = e.currentTarget.dataset.taskid;
    let data_index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '取消任务',
      content: '您确定取消任务吗？',
      success: (res) => {
        if (res.confirm) {
          wx.request({
            url: config.refundTaskUrl,
            method: 'POST',
            data: {
              task_id: taskid
            },
            header: {
              'Authorization': 'Bearer ' + wx.getStorageSync('token')
            },
            success: (res) => {
              // console.log(res)
              that.setData({
                refund_fee: res.data.refund_fee,
                order_id: res.data.out_trade_no,
                appid: res.data.pro_appid
              })
              wx.request({
                url: 'https://api.mokyun.com/wechat/refund',
                method: 'POST',
                data: {
                  refund_fee: that.data.refund_fee,
                  order_id: that.data.order_id,
                  appid: that.data.appid
                },
                header: {
                  'Authorization': 'Bearer ' + wx.getStorageSync('token')
                },
                success: (res) => {
                  // console.log(res)
                  wx.showToast({
                    title: '申请退款中',
                    icon: 'none',
                    duratioin: 1500,
                  });
                  let waitReceiveArr = that.data.waitReceive;
                  waitReceiveArr.splice(data_index, 1);
                  that.setData({
                    waitReceive: waitReceiveArr
                  })
                }
              })
            }
          })
        }
      }
    })
  },

  // 继续付款
  goPay: function(e) {
    let that = this;
    let taskId = e.currentTarget.dataset.taskid;
    let price = e.currentTarget.dataset.price;
    let data_index = e.currentTarget.dataset.index;
    that.setData({
      currentPay: that.data.waitPay[data_index]
    });
    wx.request({
      url: config.tradenoTask,
      method: 'POST',
      data: {
        task_id: taskId
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token'),
        APPID: 28
      },
      success: function(res) {
        // console.log(res)
        that.setData({
          orderId: res.data
        })
        // 支付
        wx.request({
          url: 'https://api.mokyun.com/wechat/pay',
          method: 'POST',
          data: {
            "tradeNo": that.data.orderId
          },
          header: {
            'Authorization': 'Bearer ' + wx.getStorageSync('token'),
            'APPID': 28
          },
          success: function(res) {
            // console.log(res)
            wx.requestPayment({
              timeStamp: res.data.timeStamp,
              nonceStr: res.data.nonceStr,
              package: res.data.package,
              signType: res.data.signType,
              paySign: res.data.paySign,
              success: function(res) {
                wx.request({
                  url: config.orderStatus,
                  method: 'POST',
                  data: {
                    task_id: taskId
                  },
                  header: {
                    'Authorization': 'Bearer ' + wx.getStorageSync('token'),
                    'APPID': 28
                  },
                  success: function(res) {
                    let receiver = that.data.waitReceive;
                    let pay = that.data.waitPay;
                    receiver.push(that.data.currentPay);
                    pay.splice(data_index, 1);
                    wx.showToast({
                        title: '支付成功',
                        icon: 'none',
                        duration: 1500,
                      }),
                      that.setData({
                        waitPay: pay,
                        waitReceive: receiver
                      })
                  }
                })
              },
              fail: function() {
                wx.showToast({
                  title: '支付失败',
                  icon: 'none',
                  duration: 1500,
                })
              }
            })
          }
        })
      }
    })
  },

  // 取消未支付的订单
  cancelUnpaidTask: function(e) {
    let that = this;
    let taskId = e.currentTarget.dataset.taskid;
    let data_index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '取消订单',
      content: '您确定要取消未支付的订单吗？',
      success: (res) => {
        if (res.confirm) {
          wx.request({
            url: config.cancelTaskUrl,
            method: 'POST',
            data: {
              task_id: taskId
            },
            header: {
              'Authorization': 'Bearer ' + wx.getStorageSync('token'),
              APPID: 28
            },
            success: (res) => {
              let waitPay = that.data.waitPay;
              waitPay.splice(data_index, 1);
              that.setData({
                waitPay: waitPay
              });
              wx.showToast({
                title: '取消成功',
                icon: 'none',
                duration: 1500
              })
            }
          })

        }
      }
    })
  },

  //确认任务
  finishTask: function(e) {
    let that = this;
    let taskId = e.currentTarget.dataset.id;
    let data_index = e.currentTarget.dataset.index;
    that.setData({
      currentIng: that.data.ing[data_index]
    })
    wx.request({
      url: config.finishTask,
      method: 'POST',
      data: {
        task_id: e.currentTarget.dataset.id
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token'),
        APPID: 28
      },
      success: (res) => {
        console.log(res)
        let end = that.data.end;
        let ingArr = that.data.ing;
        end.push(that.data.currentIng);
        ingArr.splice(data_index, 1);
        that.setData({
          ing: ingArr,
          end: end
        })
      }
    })
  },


  swiperTab: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    })
  },
  clickTab: function(e) {
    var that = this;
    if (that.data.currentTab == e.currentTarget.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.current
      })
    }
  },
  goTaskDetails: function() {
    wx.navigateTo({
      url: '../task-details/task-details',
    })
  }
})